<?php

namespace App\Http\Requests\Plugs\WaterStation;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;

class WaterStationMiniAppOrderRequest extends FormRequest
{

    public function __construct()
    {
        parent::__construct();
        // 添加自己的验证规则
        Validator::extend('check_delivery_time', function ($attribute, $value, $parameters) {
            if ($value == 2){// 传递来的 delivery_mode 配送方式 1自提 2送货上门 就必须填写 送货开始时间，结束时间
                if (!self::capture()->delivery_time){// 没有传递送货时间
                    return false;
                }
            }
            return true;
        });
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'product_id' => [
                'required',
            ],
            'delivery_mode' => [
                'required',
                'checkDeliveryTime'
            ],
            'sum' => [
                'required',
            ],
            'price' => [
                'required',
            ],
        ];
    }

    public function attributes()
    {
        return [
            'order_number'=>'订单号',
            'price'=>'价格',
            'product_id'=>'商品',
            'delivery_mode'=>' 配送方式',
            'sum'=>'总价',
        ];
    }

    public function messages()
    {
        return [
            'delivery_mode.check_delivery_time'=>'请填写送货时间',
        ];
    }
}
